﻿create table AspnetUsersToDelete (UserID uniqueidentifier NOT NULL PRIMARY KEY)

DECLARE @NoOfUsersToDelete int
SET @NoOfUsersToDelete = 1000

insert into AspnetUsersToDelete
select top(@NoOfUsersToDelete) UserID from aspnet_Users order by UserID -- Saves SQL Server from sorting in clustered index again
print 'Users to delete: ' + convert(varchar(255),@@ROWCOUNT)
GO

-- Delete profile of users
delete from aspnet_Profile WHERE UserID IN
( 
	SELECT UserID FROm AspnetUsersToDelete 
)
GO

-- Delete from aspnet_UsersInRoles
delete from aspnet_UsersInRoles WHERE UserID IN
(
	SELECT UserID FROm AspnetUsersToDelete
)
GO

-- Delete from aspnet_PersonalizationPerUser
delete from aspnet_PersonalizationPerUser WHERE UserID IN
(
	SELECT UserID FROm AspnetUsersToDelete 
)
GO

delete from aspnet_membership where userID IN
(
	SELECT UserID FROm AspnetUsersToDelete
)
GO

-- Delete the users
delete from aspnet_users where userID IN
(
	SELECT UserID FROm AspnetUsersToDelete
)

PRINT 'Users deleted: ' + convert(varchar(255), @@ROWCOUNT)
GO

drop table AspnetUsersToDelete
GO